package com.tongzhan.controller;

import com.tongzhan.base.BaseController;
import com.tongzhan.base.Return;
import com.tongzhan.entity.qiaotai.OpinionCar;
import com.tongzhan.entity.qiaotai.Policy;
import com.tongzhan.param.OpinionCarParam;
import com.tongzhan.param.PolicyParam;
import com.tongzhan.utils.SnowFlakeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * 政策法规
 *
 * @author htmic
 * @date 2020/8/3
 */
@RequestMapping("/policy")
@RestController
public class PolicyController extends BaseController {

    /**
     * 民意直通车列表
     *
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/policyList")
    public Return list(Integer page, Integer size) {
        page = page == null ? 0 : page - 1;
        Page<Policy> list = policyService.getList(page, size);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list.getContent());
        map.put("total", list.getTotalElements());
        map.put("totalpage", list.getTotalPages());
        return Return.getSuccessResult(map);
    }

    /**
     * 添加
     *
     * @param param
     * @return
     */
    @PostMapping("/addPolicy")
    public Return add(@RequestBody PolicyParam param) {
        if (param == null || param.getTitle() == null
                || param.getContent() == null || param.getPicture() == null) {
            return Return.getErrorResult("参数为空");
        }
        if (param.getTitle().length() > 200) {
            return Return.getErrorResult("标题过长");
        }

        Policy entity = new Policy();
        BeanUtils.copyProperties(param, entity);
        entity.setId(new SnowFlakeUtils(3, 3).nextId() + "");
        entity.setAddUserId(param.getUserId());
        long l = policyService.add(entity);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }


    /**
     * 详情
     *
     * @param id
     * @return
     */
    @PostMapping("/policyDetail")
    public Return getDetail(String id,String userId) {
        if (id == null) {
            return Return.getErrorResult("缺少id参数!");
        }
        Policy vo = policyService.getDetail(id,userId);
        return Return.getSuccessResult(vo);
    }

    /**
     * 删除政策法规
     * @param id
     * @return
     */
    @PostMapping("/delPolicy")
    public Return delPolicy(String id) {
        if (id == null) {
            return Return.getErrorResult("缺少参数");
        }
        long l = policyService.delPolicy(id);
        if (l > 0) {
            return Return.getSuccessResult("删除成功!");
        }
        return Return.getErrorResult("删除失败!");
    }
}
